import React, { useEffect, useState, useRef } from 'react';
import _ from 'lodash';

function useTableSearch({ tableState, refreshTable, updatePageInfo }) {
    // const [ keyword, setkeyword ] = useState("");
    const isManul = useRef(false);
    const timer = useRef();

    useEffect(() => {
        if (!isManul.current) return;
        if (timer.current) {
            window.clearTimeout(timer.current);
            timer.current = null;
        }
        timer.current = setTimeout(() => {
            onSearch();
            window.clearTimeout(timer.current);
            timer.current = null;
        }, 600)
    }, [])

    const onChange = (e) => {
        isManul.current = true;
        // const _keyword = e.target.value;
        // setkeyword(_keyword);
    }

    const onSearch = () => {
        refreshTable({ search: keyword }, { action: 'search' });
    }


    return { onChange, onSearch };
}

export default useTableSearch;